package com.crl.rcdp.sys.dao;

import com.cars.util.page.PageUtil;
import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import com.crl.rcdp.sys.model.SysUser;
import com.crl.rcdp.sys.vo.SysUserSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 11:10
 */
@Repository
public class SysUserDao extends BaseDao<SysUser, SysUserSearchVO> {

    /**
     * 查询全部，用于选择角色
     */
    public List<SysUser> list(SysUserSearchVO sysUserSearchVO) {
        String sql = "SELECT * FROM td_sys_user where 1=1 ";
        sql += createSQL(sysUserSearchVO);
        sql += " order by created_at";
        sql = PageUtil.createMysqlPageSql(sql, sysUserSearchVO.getPage());
        return list(sql, sysUserSearchVO);
    }

    /**
     * 增加角色
     */
    public int add(SysUser sysUser) {
        String sql = "INSERT INTO td_sys_user (username,real_name,mobile,created_by,created_at)" +
                " VALUES(:username,:realName,:mobile,:createdBy,now())";
        return insertForId(sql, sysUser, "id");
    }

    /**
     * 根据角色id删除
     */
    public int delete(Integer id) {
        String sql = "DELETE FROM td_sys_user WHERE id=?";
        return update(sql, id);
    }

    /**
     * 根据角色id修改
     */
    public int update(SysUser sysUser) {
        String sql = "UPDATE td_sys_user SET real_name=:realName,mobile=:mobile,modified_by=:modifiedBy,modified_at=now() WHERE id=:id ";
        return update(sql, sysUser);
    }

    /**
     * 统计查询数据条数
     */
    public int count(SysUserSearchVO sysUserSearchVO) {
        String sql = "SELECT COUNT(id) FROM td_sys_user WHERE 1=1 ";
        sql += createSQL(sysUserSearchVO);
        return count(sql, sysUserSearchVO);
    }

    /**
     * 拼接查询条件
     */
    private String createSQL(SysUserSearchVO sysUserSearchVO) {
        String sql = "";
        if (StringUtil.isNotNullOrEmpty(sysUserSearchVO.getUsername())) {
            sql += " and username =:username";
        }
        return sql;
    }

    /**
     * 根据id查询
     */
    public SysUser get(Integer id) {
        String sql = "SELECT * FROM td_sys_user WHERE id=?";
        return get(sql, id);
    }

    /**
     * 在线人数
     *
     * @param mitutes
     * @return
     */
    public List<SysUser> listOnline(int mitutes, SysUserSearchVO sysUserSearchVO) {
        String sql = "select u.id,u.username,u.mobile,u.real_name from td_sys_user_login t,td_sys_user u where t.user_id=u.id and t.last_login_time>date_add(now(),interval -" + mitutes + " minute) order by last_login_time desc";
        sql = PageUtil.createMysqlPageSql(sql, sysUserSearchVO.getPage());
        return list(sql);
    }

    /**
     * 在线人数
     *
     * @param mitutes
     * @return
     */
    public int listOnlineCount(int mitutes, SysUserSearchVO sysUserSearchVO) {
        String sql = "select count(*) from td_sys_user_login t,td_sys_user u where t.user_id=u.id and t.last_login_time>date_add(now(),interval -" + mitutes + " minute)  ";
        return count(sql);
    }

}
